#!/usr/bin/env python
# -*- coding: utf-8 -*-

# @Time     :2020/10/17
# @Author   :Changshu
# @File     :Exercise_303.py

# 303. 区域和检索 - 数组不可变
# 给定一个整数数组  nums，求出数组从索引 i 到 j（i ≤ j）范围内元素的总和，包含 i、j 两点。

'''使用list将从num的开始处到该处的和存起来，用空间换时间'''
class NumArray:
	def __init__(self,nums:list):
		if nums is None:
			self.sums=None
			return
		self.sums=nums
		for i in range(1,len(nums)):
			self.sums[i]+=self.sums[i-1]

	def sumRange(self, i: int, j: int) -> int:
		if self.sums is None:
			return None
		if i==0:
			return self.sums[j]
		return self.sums[j]-self.sums[i-1]

if __name__ == '__main__':
	numArray=NumArray([-2, 0, 3, -5, 2, -1])
	print(numArray.sumRange(0,5))
